package edu.maping.easy.painter;

import java.util.ArrayList;

import processing.core.PApplet;

import controlP5.ControlP5;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.Button;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.sequence.ActionConf;
import fisica.FBody;

public class UtilPainter extends ArrayList<FBody> {

	/**
	 * 
	 */

	private static final long serialVersionUID = -8513235171931663574L;
	public ControlP5 controlP5;
	private ButtonList buttonList = null;
	GuiFactory guiFactory;
	private int y = 30;

	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

		this.controlP5 = (ControlP5) controlFacade;
		this.guiFactory = guiFactory;
		this.buttonList = buttonList;
	}

	public void setPosition(int x, int y) {
		if (buttonList != null)
			buttonList.setPosition(x, y);
	}

	public boolean add(String name, float minValue, float maxValue) {
		// Slider slider = guiFactory.nSlider(name, 0, 255, 10, 0, y, 35, 10);
		edu.maping.easy.gui.Slider slider = guiFactory.newSlider(name, 100, 20,
				maxValue / 2, maxValue, minValue);
		// slider.setGroup(controlGroup);
		slider.setStatic(false);
		buttonList.add(slider);
		add(slider);
		y += 20;
		return true;
	}

	public boolean addButton(String name) {
		// Slider slider = guiFactory.nSlider(name, 0, 255, 10, 0, y, 35, 10);
		Button button = guiFactory.newButton(name, 100, 20, 40, 20);
		// slider.setGroup(controlGroup);
		button.setStatic(false);
		buttonList.add(button);
		add(button);
		y += 20;
		return true;
	}

	public void render() {
		if (buttonList != null)
			buttonList.setDrawable(true);
	}

	public void hide() {
		if (buttonList != null)
			buttonList.setDrawable(false);
	}

	public EventPainter controlEvent(GUIEvent theEvent) {

		// if (theEvent.isController()) {
		int index = indexOf(theEvent.controller());
		if (index >= 0) {
			EventPainter eventPainter = new EventPainter();
			eventPainter.name = theEvent.name();
			FBody fBody = theEvent.controller();
			if (fBody instanceof edu.maping.easy.gui.Slider) {
				edu.maping.easy.gui.Slider slider = (edu.maping.easy.gui.Slider) fBody;
				eventPainter.value = slider.getValue();
			}

			if (fBody instanceof Button) {
				eventPainter.value = 1;
			}

			return eventPainter;
		}
		// }

		return null;

	}

	public ActionConf createActionConf() {
		ActionConf actionConf = new ActionConf();

		for (int index = 0; index < size(); index++) {
			FBody fbody = get(index);
			if (fbody instanceof Slider) {
				Slider slider = (Slider) fbody;
				actionConf.put(slider.name, slider.maxValue / 2);
			}
			if (fbody instanceof Button) {
				Button button = (Button) fbody;
				actionConf.put(button.name, 1F);
			}
		}

		return actionConf;
	}

	public void setParameter(String name, float value) {
		for (int index = 0; index < size(); index++) {
			FBody fbody = get(index);
			if (fbody instanceof Slider) {
				Slider slider = (Slider) fbody;
				if (slider.name.equals(name)) {
					slider.setValue(value);
				}
			}

		}
	}

	public void reset() {
		for (int index = 0; index < size(); index++) {
			FBody fbody = get(index);
			if (fbody instanceof Slider) {
				Slider slider = (Slider) fbody;
				slider.setValue(0);
			}
		}

	}

	public UtilPainter clone() {
		UtilPainter utilPainter = new UtilPainter();

		utilPainter.controlP5 = controlP5;
		utilPainter.guiFactory = guiFactory;
		utilPainter.buttonList = buttonList;

		utilPainter.addAll(this);

		return utilPainter;

	}
}
